import workManagement from "@/api/workManagement/index";
import eventBus from '../utils/eventBus'
import ApproverList from '../components/WorkFlowsDialog/Case/ApproverList'
import SuggestionCommonDialog from '../components/WorkFlowsDialog/Case/SuggestionCommonDialog'
import common from "@/api/common/index";
export default {
  components: { ApproverList, SuggestionCommonDialog},
  inject:{
    fullDialog:{
      type:Boolean,
      default:false
    }
  },
  props:{
    row:{
      type:Object,
      default:()=>{}
    }
  },
  data() {
    return {
      approverInfo:{
        dataList: [],
        visible: false,
        callback:()=>{},
        showCheckbox:false,
        params:{}
      },
      eventDialogInfo:{
        component:null,
        visible:true,
        callback:()=>{},
        formData:{}
      },
      currBtn:{},
      suggestionInfo:{
        visible:false,
        callback:()=>{},
        msg:""
      },
    }
  },
  created() {

  },
  computed:{
    btnList(){
      return this.$store.getters.btnList
    }
  },
  methods: {
    async doSomething(btn) {
      this.currBtn = btn
      console.log('---this.currBtn---',btn)
      if(btn.isAdd){//加签
        await this.showForwardOrAddApproverList({r:'user'})
        await this.checkSuggestionRequired()//意见处理弹框
        await this.toOtherSubmit('toAddNode')
      }else if(btn.in_add_node){//加签完成
        await this.checkSuggestionRequired()//意见处理弹框
        await this.toOtherSubmit('toAddNodeEnd')
      }else if(btn.isForwardAll){//转发所有
        await this.showForwardOrAddApproverList({r:'all',d:'all'})
        await this.checkSuggestionRequired()//意见处理弹框
        await this.toOtherSubmit('toDeliver')
      }else if(btn.isForward){//转发
        await this.showForwardOrAddApproverList({r:'user'})
        await this.checkSuggestionRequired()//意见处理弹框
        await this.toOtherSubmit('toDeliver')
      }else if(btn.is_accept){//接单
        await this.checkSuggestionRequired()//意见处理弹框
        await this.toOtherSubmit('toAccept')
      }else{
        await this.showApproverList()
        await this.checkSuggestionRequired()//意见处理弹框
        await this.showDiyPop()//自定义弹框
        await this.handleBtnEvent()//请求配置的接口地址
        await this.toSubmit()//提交工单
      }
    },
    checkSuggestionRequired(){
      if(!this.fullDialog){//超简单弹框，如流程图上有弹框啥也不用干
        return new Promise((resolve, reject)=>{
          this.suggestionInfo.visible = true;
          this.suggestionInfo.callback = ()=>{

            resolve();
          }
        })
      }
    },
    showForwardOrAddApproverList(data){//加签和转交选人
      return new Promise((resolve, reject)=>{
        common.getAccountSelect(data).then(res => {
          this.approverInfo.dataList = res.data
          this.approverInfo.visible=true
          this.approverInfo.showCheckbox=this.currBtn.select_checkbox
          this.approverInfo.callback=(data)=>{
            this.username = data.username.split("-")[0];//解决键不维一的情况
            this.approverInfo.params['target_username'] = this.username;
            resolve()
          }
        }).catch(()=>{reject('接口请求错误！');})
      })
    },
    showApproverList(){//其它配置的按扭选人弹框
      return new Promise((resolve, reject)=>{
        if(this.currBtn.select_url){
          workManagement.getSomething(this.currBtn.select_url).then(res => {
            this.approverInfo.dataList = res.data
            this.approverInfo.visible=true
            this.approverInfo.showCheckbox=this.currBtn.select_checkbox
            this.approverInfo.callback=(data)=>{
              this.username = data.username.split("-")[0];//解决键不维一的情况
              this.approverInfo.params[this.currBtn.select_key] = this.username;
              resolve()
            }
          }).catch(()=>{reject('配置的选择接口请求错误！');})
        }else{
          resolve()
        }
      })
    },
    showDiyPop(){//自定义弹框
      return new Promise((resolve, reject)=>{
        if(this.currBtn.diy_pop){
          let diy_pop = require(`@/views/${this.currBtn.diy_pop}`)
          this.eventDialogInfo.component = diy_pop.default;
          this.eventDialogInfo.visible = true;
          this.eventDialogInfo.callback = (data)=>{
            this.eventDialogInfo.formData = data
            resolve();
          }
        }else{
          resolve();
        }
      })
    },
    handleBtnEvent() {
      return new Promise((resolve, reject)=>{

        if (this.currBtn.get_url) {
          let url = this.currBtn.get_url;
          url = url.replace('{id}', this.row.id);//此处id写死，不支持传其它参数
          if(this.currBtn.diy_pop){
            workManagement.getSomethingPost(url,this.eventDialogInfo.formData).then(res => {
              resolve();
            }).catch(()=>{reject('配置的接口请求错误！');})
          }else{
            workManagement.getSomething(url).then(res => {
              resolve();
            }).catch(()=>{reject('配置的接口请求错误！');})
          }
        } else {
          resolve();
        }
      })
    },
    toSubmit() {
      if(this.fullDialog)this.suggestionInfo.msg = this.$store.getters.msg
      if(this.fullDialog&&this.currBtn.is_required&&!this.suggestionInfo.msg){//流程图上有意见框且意见必填
        this.msgError('意见必填，请输入意见！');
        return
      }
      return new Promise((resolve, reject)=>{
        workManagement.toTicketsDandle(this.row.ticket, {
          transition_id: this.currBtn.transition_id,
          suggestion: this.suggestionInfo.msg,
          ...this.approverInfo.params
        }).then(res => {
          this.msgSuccess(res.msg);
          eventBus.$emit("closeDetailDialog",true)
          resolve();
        }).catch(()=>{reject('工单处理失败！');});
      })
    },
    toOtherSubmit(funName){//加签，加签完成，转交，接单统一走这个方法
      return new Promise((resolve, reject)=>{
        workManagement[funName](this.row.ticket, {
          suggestion: this.suggestionInfo.msg,
          ...this.approverInfo.params
        }).then(res => {
          this.msgSuccess(res.msg);
          eventBus.$emit("closeDetailDialog",true)
          resolve();
        }).catch(()=>{reject('工单处理失败！');});
      })
    },
  }
}
